/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utn.tsb.tej.comunications.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import utn.tsb.tej.comunications.GameData;
import utn.tsb.tej.comunications.Commands;

/**
 * This class contains the necessary behavior to contact with a room game server
 * and get the game data.
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Client implements Runnable {

    private static String HOST = "";
    private static final int PORT = 8181;
    private static final int TIMEOUT = 500;
    private static Socket socket;
    private static OutputStream oStream;
    private static DataOutputStream dataOutput;
    private static InputStream iStream;
    private static ObjectInputStream objectInput;
    private GameData gData;
    private boolean isActionCanceled;
    private boolean joinGame;

    public Client(String host) {
        HOST = host;
        isActionCanceled = false;
        joinGame = false;
    }

    @Override
    public void run() {

        try {
            /*
             * At first we try to get the game data.
             * While the game data is not ready, and the actual action
             * weren't caceled, the Client will keep trying over and over.
             */
            while (!isGameDataReady() && !isActionCanceled) {
                openConnection();//open the streams
                getGameDataFromHost();//Ask for the game data
                closeConnection();//Close the streams
                if (!isGameDataReady()) {
                    try {
                        Thread.sleep(TIMEOUT);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    System.out.println("Recived GameData from: "+gData.getRoomName());
                }
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }


//        try {
//
//            /*
//             * At first we try to get the game data.
//             * While the game data is not ready, and the actual action
//             * weren't caceled, the Client will keep trying over and over.
//             */
//            while (!isGameDataReady() && !isActionCanceled) {
//                openConnection();//Open the streams
//                getGameDataFromHost();//Ask for the game data
//                closeConnection();//Close the streams
//                try {
//                    Thread.sleep(TIMEOUT);
//                } catch (InterruptedException ex) {
//                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            }
//
//
//            while (!joinGame) {
//                try {
//                    Thread.sleep(300);
//                } catch (InterruptedException ex) {
//                }
//            }
//
//            isActionCanceled = false;
//
//            while (!isActionCanceled) {
//
//            }
//
//
//
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (UnknownHostException ex) {
//            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    private void closeConnection() throws IOException {
        objectInput.close();
        iStream.close();
    }

    private void openConnection() throws IOException {
        //Create the socket with the HOST address and the PORT
        socket = new Socket(HOST, PORT);
        iStream = socket.getInputStream(); //Gets the input stream
        objectInput = new ObjectInputStream(iStream); //creates the objesDataOutputStreamct stream

        oStream = socket.getOutputStream();
        dataOutput = new DataOutputStream(oStream);
    }

    /**
     * Ask the host for the game data.
     */
    private void getGameDataFromHost() throws IOException, ClassNotFoundException {
        dataOutput.writeInt(Commands.GAMEDATA);//Ask for the game data
        gData = (GameData) (objectInput.readObject());//gets the game data
    }

    /**
     * Join on the room game asked before
     */
    private void joinGameHost()
    {

    }

    public boolean isGameDataReady() {
        return gData != null;
    }

    public GameData getGameData() {
        return gData;
    }

    public void joinGame() {
        joinGame = true;
        this.notify();
    }

    public void cancelActualAction() {
        isActionCanceled = true;
        this.notify();
    }
}
